package sms;

import dbmanager.DBManager;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.util.*;

public class ReceiveThread extends TimerTask
{	
	private Hashtable<Object, Object> messageDetails = new Hashtable<Object, Object>();
	DBManager database;
	RecieveSMS recieve;
	SendSMS send ;
	PortHandler handle;
	
	public ReceiveThread()
	{
		database = new DBManager();
		database.getConnect();
		recieve = new RecieveSMS();
		recieve.setDatabase(database);
		handle = recieve.getPortHandler();
		send = new SendSMS(handle);
		send .setDatabase(database);
	}
	
	public void run()
    {
		try
		{
	 		recieve.recieveSMS();
	 		send.sendSMS();
	 		{
				try
				{
					System.out.println("mobile::ReceiveThread::run()::I'm in rec thread");
					ResultSet rs = database.getSelect("SELECT * FROM sms where send = false and status =0");
					while(rs.next())
					{
						boolean FLAG = true;
						String multiple = rs.getString("multiple");
						String datetime = rs.getString("datetime");
						String username1 = rs.getString("mobile");
						String MsgText =rs.getString("message");
						String id =  rs.getString("id");
						if(multiple.equals("1"))
						{
							FLAG = false;
							ResultSet rs1 = database.getSelect("SELECT * FROM sms where send = false and status =0 and datetime = '"+datetime+"' and mobile = '"+username1 + "' and id>"+id+" order by id");
							while(rs1.next())
							{
								id = id + "," + rs1.getString("id") ; 
								MsgText = MsgText + rs1.getString("message");
								MsgText = MsgText.substring(0,MsgText.length()-1);
								//rs1.updateString("status", "1");
							}
							if(id.contains(","))
							{
								FLAG = true;
							}
							else
							{
								rs1 = database.getSelect("SELECT * FROM sms where send = false and status =0 and datetime > '"+datetime+"' and mobile = '"+username1 + "' order by id");
								if(rs1.next())
								{
									FLAG = true;
								}
							}
						}
						if(FLAG)
						{
							System.out.println("Username : "+username1);
							System.out.println("MessageText : "+MsgText);
							messageDetails.put("Username", username1);
							messageDetails.put("MessageText", MsgText);
//							new ClientThread(messageDetails);
							//rs.updateString("status", "1");
							database.getUpdate("update sms set status = 1 where id in( "+ id +")");
						}
					}
					rs.close();
				}
				catch (Exception e)
				{
					System.out.println("mobile::ReceiveThread::run()::Server is in waiting state");
					e.printStackTrace();
				}
			}
			
		}
		catch (Exception e)
		{
			System.out.println("mobile::ReceiveThread::run()::!!!!!!!! Exception \n");
			e.printStackTrace();
		}
	}
	public static void main(String[] args) 
	{
		ReceiveThread rt = new ReceiveThread();
		Timer time = new Timer();
		time.schedule(rt, 0,2000);
		
	}
	
 }


class ClientThread extends Thread
{

//	Connection Data
 	private BufferedReader inputPort;
	private PrintStream outputport;
	private String Username, Request;
	
	ClientThread(Hashtable<Object, Object> Message)
	{
		Username = Message.get("Username").toString();
		Request = Message.get("MessageText").toString();
		start();
	}
	public void run()
	{
		System.out.println("mobile::ClientThread::run()::I'm in receive thread");
		try
		{
		    //Socket socket = new Socket("192.168.0.127",9999);
			Socket socket = new Socket("localhost",9999);
			outputport = new PrintStream(socket.getOutputStream());
			System.out.println("mobile::ClientThread::run()::Sending User --> "+Username);
			System.out.println("mobile::ClientThread::run()::Sending Message --> "+Request);
			outputport.println(Username);
			outputport.flush();
			outputport.println(Request);
			outputport.flush();
			inputPort = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String input = inputPort.readLine();
			System.out.println("mobile::ClientThread::run()::Reply Message --> "+input);
		}	
		catch(Exception e)
		{
			System.out.println("mobile::ClientThread::run()::Unable to connect Server");
			e.printStackTrace();
		}
	}
		
}
